function varargout = SetCam(varargin)
% SETCAM M-file for SetCam.fig
%      SETCAM, by itself, creates a new SETCAM or raises the existing
%      singleton*.
%
%      H = SETCAM returns the handle to a new SETCAM or the handle to
%      the existing singleton*.
%
%      SETCAM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SETCAM.M with the given input arguments.
%
%      SETCAM('Property','Value',...) creates a new SETCAM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SetCam_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SetCam_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help SetCam

% Last Modified by GUIDE v2.5 02-Jan-2011 18:22:52

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SetCam_OpeningFcn, ...
                   'gui_OutputFcn',  @SetCam_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before SetCam is made visible.
function SetCam_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SetCam (see VARARGIN)

% Choose default command line output for SetCam
% handles.output = hObject;
axis(handles.roi_view,[0,2048,0,2048]) 
axis ij ;


str = which('SetCam');str=str(1:end-2);
filename=[str,'config.mat'];
if exist(filename,'file')    
    load(filename);
    handles.output=output;% load from setcamconfig    
    Self_SetupCombines(handles,output);
    if isfield(output,'roi_hori_min')
        rectangle('Position',[output.roi_hori_min,output.roi_vert_min,(output.roi_hori_max-output.roi_hori_min),(output.roi_vert_max-output.roi_vert_min)]);
    end
else
    handles.output=[];
end

handles.XLM = get(handles.roi_view,'xlim');
handles.YLM = get(handles.roi_view,'ylim');
handles.AXP=get(handles.roi_view,'pos');
handles.last=0;
handles.drawrect=0;
guidata(hObject, handles);


% UIWAIT makes SetCam wait for user response (see UIRESUME)
uiwait(handles.ROI);


% --- Outputs from this function are returned to the command line.
function varargout = SetCam_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
output=handles.output;
output=Self_GetEditValue(handles,output);
str = which('SetCam');str=str(1:end-2);
save([str,'config.mat'],'output');
varargout{1} = output;
delete(hObject);

% --- Executes on selection change in trigmode.
function trigmode_Callback(hObject, eventdata, handles)
% hObject    handle to trigmode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.trigmode=1;
    case 2
        handles.output.trigmode=2;
    case 3
        handles.output.trigmode=3;
    case 4
        handles.output.trigmode=4;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns trigmode contents as cell array
%        contents{get(hObject,'Value')} returns selected item from trigmode


% --- Executes during object creation, after setting all properties.
function trigmode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to trigmode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function exposure_Callback(hObject, eventdata, handles)
% hObject    handle to exposure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of exposure as text
%        str2double(get(hObject,'String')) returns contents of exposure as a double


% --- Executes during object creation, after setting all properties.
function exposure_CreateFcn(hObject, eventdata, handles)
% hObject    handle to exposure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function delay_Callback(hObject, eventdata, handles)
% hObject    handle to delay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of delay as text
%        str2double(get(hObject,'String')) returns contents of delay as a double


% --- Executes during object creation, after setting all properties.
function delay_CreateFcn(hObject, eventdata, handles)
% hObject    handle to delay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in bin_horizon.
function bin_horizon_Callback(hObject, eventdata, handles)
% hObject    handle to bin_horizon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.bin_horizon=1;
    case 2
        handles.output.bin_horizon=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns bin_horizon contents as cell array
%        contents{get(hObject,'Value')} returns selected item from bin_horizon


% --- Executes during object creation, after setting all properties.
function bin_horizon_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bin_horizon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in bin_vertical.
function bin_vertical_Callback(hObject, eventdata, handles)
% hObject    handle to bin_vertical (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.bin_vertical=1;
    case 2
        handles.output.bin_vertical=2;
    case 3
        handles.output.bin_vertical=3;
    case 4
        handles.output.bin_vertical=4;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns bin_vertical contents as cell array
%        contents{get(hObject,'Value')} returns selected item from bin_vertical


% --- Executes during object creation, after setting all properties.
function bin_vertical_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bin_vertical (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in recordmode.
function recordmode_Callback(hObject, eventdata, handles)
% hObject    handle to recordmode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.recordmode=1;
    case 2
        handles.output.recordmode=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns recordmode contents as cell array
%        contents{get(hObject,'Value')} returns selected item from recordmode


% --- Executes during object creation, after setting all properties.
function recordmode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to recordmode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in acquiremode.
function acquiremode_Callback(hObject, eventdata, handles)
% hObject    handle to acquiremode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.acquiremode=1;
    case 2
        handles.output.acquiremode=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns acquiremode contents as cell array
%        contents{get(hObject,'Value')} returns selected item from acquiremode


% --- Executes during object creation, after setting all properties.
function acquiremode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to acquiremode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in timestamp.
function timestamp_Callback(hObject, eventdata, handles)
% hObject    handle to timestamp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.timestamp=1;
    case 2
        handles.output.timestamp=2;
    case 3
        handles.output.timestamp=3;
    case 4
        handles.output.timestamp=4;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns timestamp contents as cell array
%        contents{get(hObject,'Value')} returns selected item from timestamp


% --- Executes during object creation, after setting all properties.
function timestamp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to timestamp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in adconverter.
function adconverter_Callback(hObject, eventdata, handles)
% hObject    handle to adconverter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.adconverter=1;
    case 2
        handles.output.adconverter=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns adconverter contents as cell array
%        contents{get(hObject,'Value')} returns selected item from adconverter


% --- Executes during object creation, after setting all properties.
function adconverter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to adconverter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in pixelclock.
function pixelclock_Callback(hObject, eventdata, handles)
% hObject    handle to pixelclock (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.pixelclock=1;
    case 2
        handles.output.pixelclock=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns pixelclock contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pixelclock


% --- Executes during object creation, after setting all properties.
function pixelclock_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pixelclock (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% % --- Executes on selection change in popupmenu11.
% function popupmenu11_Callback(hObject, eventdata, handles)
% % hObject    handle to popupmenu11 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: contents = get(hObject,'String') returns popupmenu11 contents as cell array
% %        contents{get(hObject,'Value')} returns selected item from popupmenu11
% 
% 
% % --- Executes during object creation, after setting all properties.
% function popupmenu11_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to popupmenu11 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: popupmenu controls usually have a white background on Windows.
% %       See ISPC and COMPUTER.
% if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
%     set(hObject,'BackgroundColor','white');
% end


% --- Executes on selection change in doubleimage.
function doubleimage_Callback(hObject, eventdata, handles)
% hObject    handle to doubleimage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.doubleimage=1;
    case 2
        handles.output.doubleimage=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns doubleimage contents as cell array
%        contents{get(hObject,'Value')} returns selected item from doubleimage


% --- Executes during object creation, after setting all properties.
function doubleimage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to doubleimage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in bwnoise.
function bwnoise_Callback(hObject, eventdata, handles)
% hObject    handle to bwnoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.bwnoise=1;
    case 2
        handles.output.bwnoise=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns bwnoise contents as cell array
%        contents{get(hObject,'Value')} returns selected item from bwnoise


% --- Executes during object creation, after setting all properties.
function bwnoise_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bwnoise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% % --- Executes on selection change in popupmenu14.
% function popupmenu14_Callback(hObject, eventdata, handles)
% % hObject    handle to popupmenu14 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% % Hints: contents = get(hObject,'String') returns popupmenu14 contents as cell array
% %        contents{get(hObject,'Value')} returns selected item from popupmenu14
% 
% 
% % --- Executes during object creation, after setting all properties.
% function popupmenu14_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to popupmenu14 (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    empty - handles not created until after all CreateFcns called
% 
% % Hint: popupmenu controls usually have a white background on Windows.
% %       See ISPC and COMPUTER.
% if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
%     set(hObject,'BackgroundColor','white');
% end


% --- Executes on selection change in sensorformat.
function sensorformat_Callback(hObject, eventdata, handles)
% hObject    handle to sensorformat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.sensorformat=1;
    case 2
        handles.output.sensorformat=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns sensorformat contents as cell array
%        contents{get(hObject,'Value')} returns selected item from sensorformat


% --- Executes during object creation, after setting all properties.
function sensorformat_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sensorformat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Comfirm.
function Comfirm_Callback(hObject, eventdata, handles)
% hObject    handle to Comfirm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiresume();


function roi_vert_min_Callback(hObject, eventdata, handles)
% hObject    handle to roi_vert_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=str2num(get(handles.roi_hori_min,'string'));
w=str2num(get(handles.roi_hori_max,'string'))-x;
y=str2num(get(handles.roi_vert_min,'string'));
h=str2num(get(handles.roi_vert_max,'string'))-y;
if w<=0
    w=1;
end
if h<=0
    h=1;
end
cla(handles.roi_view);
rectangle('Position',[x,y,w,h]);
% Hints: get(hObject,'String') returns contents of roi_vert_min as text
%        str2double(get(hObject,'String')) returns contents of roi_vert_min as a double


% --- Executes during object creation, after setting all properties.
function roi_vert_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roi_vert_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function roi_vert_max_Callback(hObject, eventdata, handles)
% hObject    handle to roi_vert_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=str2num(get(handles.roi_hori_min,'string'));
w=str2num(get(handles.roi_hori_max,'string'))-x;
y=str2num(get(handles.roi_vert_min,'string'));
h=str2num(get(handles.roi_vert_max,'string'))-y;
if w<=0
    w=1;
end
if h<=0
    h=1;
end
cla(handles.roi_view);
rectangle('Position',[x,y,w,h]);
% Hints: get(hObject,'String') returns contents of roi_vert_max as text
%        str2double(get(hObject,'String')) returns contents of roi_vert_max as a double


% --- Executes during object creation, after setting all properties.
function roi_vert_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roi_vert_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function roi_hori_min_Callback(hObject, eventdata, handles)
% hObject    handle to roi_hori_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=str2num(get(handles.roi_hori_min,'string'));
w=str2num(get(handles.roi_hori_max,'string'))-x;
y=str2num(get(handles.roi_vert_min,'string'));
h=str2num(get(handles.roi_vert_max,'string'))-y;
if w<=0
    w=1;
end
if h<=0
    h=1;
end
cla(handles.roi_view);
rectangle('Position',[x,y,w,h]);
% Hints: get(hObject,'String') returns contents of roi_hori_min as text
%        str2double(get(hObject,'String')) returns contents of roi_hori_min as a double


% --- Executes during object creation, after setting all properties.
function roi_hori_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roi_hori_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function roi_hori_max_Callback(hObject, eventdata, handles)
% hObject    handle to roi_hori_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

x=str2num(get(handles.roi_hori_min,'string'));
w=str2num(get(handles.roi_hori_max,'string'))-x;
y=str2num(get(handles.roi_vert_min,'string'));
h=str2num(get(handles.roi_vert_max,'string'))-y;
if w<=0
    w=1;
end
if h<=0
    h=1;
end
cla(handles.roi_view);
rectangle('Position',[x,y,w,h]);
% Hints: get(hObject,'String') returns contents of roi_hori_max as text
%        str2double(get(hObject,'String')) returns contents of roi_hori_max as a double


% --- Executes during object creation, after setting all properties.
function roi_hori_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to roi_hori_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in settomax.
function settomax_Callback(hObject, eventdata, handles)
% hObject    handle to settomax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.roi_vert_min,'string', '1');
set(handles.roi_vert_max,'string', '2048');
set(handles.roi_hori_min,'string', '1');
set(handles.roi_hori_max,'string', '2048');
cla(handles.roi_view);
rectangle('Position',[1,1,2048,2048]);


function conversion_Callback(hObject, eventdata, handles)
% hObject    handle to conversion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of conversion as text
%        str2double(get(hObject,'String')) returns contents of conversion as a double


% --- Executes during object creation, after setting all properties.
function conversion_CreateFcn(hObject, eventdata, handles)
% hObject    handle to conversion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function cooling_Callback(hObject, eventdata, handles)
% hObject    handle to cooling (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of cooling as text
%        str2double(get(hObject,'String')) returns contents of cooling as a double


% --- Executes during object creation, after setting all properties.
function cooling_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cooling (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in offsetctrl.
function offsetctrl_Callback(hObject, eventdata, handles)
% hObject    handle to offsetctrl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val=get(hObject,'Value');
switch (val)
    case 1
        handles.output.offsetctrl=1;
    case 2
        handles.output.offsetctrl=2;
end
guidata(hObject,handles);
% Hints: contents = get(hObject,'String') returns offsetctrl contents as cell array
%        contents{get(hObject,'Value')} returns selected item from offsetctrl


% --- Executes during object creation, after setting all properties.
function offsetctrl_CreateFcn(hObject, eventdata, handles)
% hObject    handle to offsetctrl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when user attempts to close ROI.
function ROI_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
% delete(hObject);
uiresume();


function Self_SetupCombines(handles,output)
% pop up menu
if isfield(output,'trigmode')
    set(handles.trigmode,'Value', output.trigmode);
end
if isfield(output,'recordmode')
    set(handles.recordmode,'Value', output.recordmode);
end
if isfield(output,'acquiremode')
    set(handles.acquiremode,'Value', output.acquiremode);
end
if isfield(output,'timestamp')
    set(handles.timestamp,'Value', output.timestamp);
end
if isfield(output,'bin_horizon')
    set(handles.bin_horizon,'Value', output.bin_horizon);
end
if isfield(output,'bin_vertical')
    set(handles.bin_vertical,'Value', output.bin_vertical);
end
if isfield(output,'adconverter')
    set(handles.adconverter,'Value', output.adconverter);
end
if isfield(output,'pixelclock')
    set(handles.pixelclock,'Value', output.pixelclock);
end
if isfield(output,'doubleimage')
    set(handles.doubleimage,'Value', output.doubleimage);
end
if isfield(output,'offsetctrl')
    set(handles.offsetctrl,'Value', output.offsetctrl);
end
if isfield(output,'bwnoise')
    set(handles.bwnoise,'Value', output.bwnoise);
end
if isfield(output,'sensorformat')
    set(handles.sensorformat,'Value', output.sensorformat);
end

% edit
if isfield(output,'exposure')
    set(handles.exposure,'string', sprintf('%6.6f',output.exposure));
end
if isfield(output,'delay')
    set(handles.delay,'string', sprintf('%6.6f',output.delay));
end
if isfield(output,'conversion')
    set(handles.conversion,'string', sprintf('%6.2f',output.conversion));
end
if isfield(output,'cooling')
    set(handles.cooling,'string', sprintf('%2.2f',output.cooling));
end
if isfield(output,'roi_vert_min')
    set(handles.roi_vert_min,'string', int2str(output.roi_vert_min));
end
if isfield(output,'roi_vert_max')
    set(handles.roi_vert_max,'string', int2str(output.roi_vert_max));
end
if isfield(output,'roi_hori_min')
    set(handles.roi_hori_min,'string', int2str(output.roi_hori_min));
end
if isfield(output,'roi_hori_max')
    set(handles.roi_hori_max,'string', int2str(output.roi_hori_max));
end

function output=Self_GetEditValue(handles,output)
% exposure
str= get(handles.exposure,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.exposure=dat;
    else
        output.exposure=10;
    end
end
% delay
str= get(handles.delay,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.delay=dat;
    else
        output.delay=0;
    end
end
% conversion
str= get(handles.conversion,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.conversion=dat;
    else
        output.conversion=1;
    end
end
% cooling
str= get(handles.cooling,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.cooling=dat;
    else
        output.cooling=10;
    end
end
% roi_vert_min
str= get(handles.roi_vert_min,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.roi_vert_min=int32(dat);
    else
        output.roi_vert_min=1;
    end
end
% roi_vert_max
str= get(handles.roi_vert_max,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.roi_vert_max=int32(dat);
    else
        output.roi_vert_max=2048;
    end
end
% roi_hori_min
str= get(handles.roi_hori_min,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.roi_hori_min=int32(dat);
    else
        output.roi_hori_min=1;
    end
end
% roi_hori_max
str= get(handles.roi_hori_max,'string');
if ~isempty(str) 
    dat=(str2double(str));
    if ~isnan(dat)
        output.roi_hori_max=int32(dat);
    else
        output.roi_hori_max=2048;
    end
end


% --- Executes on mouse press over axes background.
function roi_view_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to roi_view (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.drawrect=1;
handles.firstpos = int32(get(hObject,'currentpoint'));
set(handles.roi_vert_min,'string',int2str(handles.firstpos(3)));
set(handles.roi_hori_min,'string',int2str(handles.firstpos(1)));
guidata(hObject,handles);
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function ROI_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on mouse motion over figure - except title and menu.
function ROI_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
F = get(hObject,'currentpoint'); % The current point w.r.t the figure.
% Figure out of the current point is over the axes or not -> logicals.
tf1 = handles.AXP(1) <= F(1) && F(1) <= handles.AXP(1) + handles.AXP(3);
tf2 = handles.AXP(2) <= F(2) && F(2) <= handles.AXP(2) + handles.AXP(4);
if tf1 && tf2 
    if handles.last~=1
        handles.last=1;
        guidata(hObject,handles);
        set(gcf,'Pointer','crosshair');
    end
    if handles.drawrect==1
        F=get(handles.roi_view,'currentpoint');
        x=min(handles.firstpos(1),F(1));
        y=min(handles.firstpos(3),F(3));
        w=abs(F(1)-handles.firstpos(1));
        if w<= 0
            w=1;
        end
        h=abs(F(3)-handles.firstpos(3));
        if h<= 0
            h=1;
        end
        cla(handles.roi_view);
        rectangle('Position',[x,y,w,h]);
        set(handles.roi_vert_max,'string',int2str(max([F(3),handles.firstpos(3)])));
        set(handles.roi_hori_max,'string',int2str(max([F(1),handles.firstpos(1)])));
        set(handles.roi_vert_min,'string',int2str(min([F(3),handles.firstpos(3)])));
        set(handles.roi_hori_min,'string',int2str(min([F(1),handles.firstpos(1)])));
    end
else
    if handles.last~=0
        handles.last=0;
        guidata(hObject,handles);
        set(gcf,'Pointer','arrow');
    end
end

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function ROI_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to ROI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.drawrect=0;
guidata(hObject,handles);
